const auth=async (ctx,next)=>{
    const {authorization}=ctx.request.header;
    if(authorization) {
        const token = authorization.split(' ')[1]
        // "Bearer xxxx"
        // try 代码运行区
        // catch 捕获错误 可以防止代码报错导致程序阻塞
        try {
            jwt.verify(token, '1812a') // 解析token
            await next()
        } catch (error) {
            ctx.status = 401;
            ctx.body ={
                code:-1,
                message:'校验失败' + error.message
            }
        }
    } else {
        ctx.status = 401;
        ctx.body ={
            code:-1,
            message:'无权限'
        }
    }
}
module.exports=auth;